package com.wuming.house.service.impl;

import com.wuming.house.core.BusinessException;
import com.wuming.house.dao.HouseCommentMapper;
import com.wuming.house.model.HouseComment;
import com.wuming.house.service.HouseCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xuhuai
 * @createTime 2019/8/30 11:21
 * @Description:
 */
@Service
public class HouseCommentServiceImpl extends BaseServiceImpl implements HouseCommentService {

    @Autowired
    private HouseCommentMapper houseCommentMapper;

    @Override
    public List<HouseComment> selectHouseCommentsByPage(Integer minHouseScore, Integer maxHouseScore, Long houseId,Long userid, int pageIndex, int pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("houseId", houseId);
        if (null != minHouseScore) {
            params.put("minHouseScore", minHouseScore);
        }
        if (null != maxHouseScore) {
            params.put("maxHouseScore", maxHouseScore);
        }
        if (null!=userid){
            params.put("userid",userid);
        }
        try {
            return (List<HouseComment>) this.getPageList(HouseCommentMapper.class, "selectHouseComments", params, pageIndex, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int selectTotal(Integer minHouseScore, Integer maxHouseScore, Long houseId,Long userid) {
        Map<String, Object> params = new HashMap<>();
        params.put("houseId", houseId);
        if (null != minHouseScore) {
            params.put("minHouseScore", minHouseScore);
        }
        if (null != maxHouseScore) {
            params.put("maxHouseScore", maxHouseScore);
        }
        if (null!=userid){
            params.put("userid",userid);
        }
        try {
            return this.getTotal(HouseCommentMapper.class, "selectTotal", params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public HouseComment selectHouseCommentById(Long id) {
        try {
            return houseCommentMapper.selectByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void saveHouseComment(HouseComment houseComment) {
        try {
            houseCommentMapper.insert(houseComment);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void modifyHouseComment(HouseComment houseComment) {
        try {
            houseCommentMapper.updateByPrimaryKey(houseComment);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Long id) {
        try {
            houseCommentMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public double getScoreByHouseComments(List<HouseComment> comments) throws BusinessException {
        int totalScore = 0;
        for (HouseComment hc : comments) {
            totalScore += hc.getHouseScore();
        }
        return totalScore * 1.00 / comments.size();
    }

    @Override
    public void modifyHuifu(HouseComment houseComment) {
        try {
            houseCommentMapper.updateHuifuByPrimaryKey(houseComment);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
